package com.allwees.bs.task.order.notify;

import cn.hutool.core.collection.CollectionUtil;
import com.allwees.bs.module.mms.dao.RetailerDao;
import com.allwees.bs.module.mms.entity.Retailer;
import com.allwees.bs.module.oms.dao.OmsOrderItemDao;
import com.allwees.bs.module.oms.entity.OmsOrderItem;
import com.allwees.core.common.util.freemarker.FreeMarkerUtil;
import com.allwees.sender.EMailSender;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 日常发货提醒
 */
@Slf4j
@Service

public class OrderShippedNotifyTask {

    @Resource
    private OmsOrderItemDao omsOrderItemDao;

    @Autowired
    private RetailerDao retailerDao;

    @Autowired
    protected EMailSender mailSender;

    @Autowired
    protected FreeMarkerUtil freeMarkerUtil;


    @XxlJob(value = "orderShippedNotifyTask")
    public ReturnT<String> run(String s) {
        doTask();
        return ReturnT.SUCCESS;
    }

    private void doTask() {
        List<OmsOrderItem> omsOrderItems = omsOrderItemDao.findUnShipped();
        if(CollectionUtil.isEmpty(omsOrderItems)){
            return;
        }
        Map<String, List<OmsOrderItem>> map = omsOrderItems.stream().collect(Collectors.groupingBy(OmsOrderItem::getRetailerUuid,Collectors.toList()));
        for(Map.Entry<String, List<OmsOrderItem>> entry:map.entrySet()) {
            Retailer retailer = retailerDao.findByUuid(entry.getKey());
            String email = retailer.getEmail();
            if (StringUtils.isBlank(email)) {
                log.info("Retailer [{}] email is null,Retailer uuid:{}", retailer.getCompanyName(),retailer.getUuid());
                continue;
            }
            List<OmsOrderItem> list = entry.getValue();
            log.info(">>> retailerUuid:{}, un_shipped order items,size={}",retailer.getUuid(),list.size());
            int afterFreezeNum = 0;
            int afterFreezeCount = 0;
            for(OmsOrderItem omsOrderItem :list) {
                if(omsOrderItem.getCanShipping()) {
                    ++afterFreezeNum;
                    afterFreezeCount = afterFreezeCount + omsOrderItem.getQuantity() ;
                }
            }
            sendOrderShippedNotifyEmail(email,afterFreezeNum,afterFreezeCount);
        }
    }

    private  void sendOrderShippedNotifyEmail(String email,Integer unShippedNum,Integer afterFreezeNum) {

        Map<String, Object> params = new HashMap<>(3);
        params.put("unShippedNum", unShippedNum);
        params.put("afterFreezeNum", afterFreezeNum);


        String templateUrl = "order_shipped_notify.ftl";
        String content = freeMarkerUtil.render(templateUrl, params);

        String subject = "发货提醒";
        mailSender.send(email, subject, content, true);
    }
}
